package com.zhangdi.demo.jdbc.controller;

import com.zhangdi.demo.jdbc.entity.TestTable;
import com.zhangdi.demo.jdbc.entity.User;
import com.zhangdi.demo.jdbc.impl.ReadService;
import com.zhangdi.demo.jdbc.impl.TestTableService;
import com.zhangdi.demo.jdbc.impl.UserService;
import com.zhangdi.demo.jdbc.vo.QVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/zz")
public class TestController2 {
    @Autowired
    ReadService readService;
    @Autowired
    UserService userService;
    @Autowired
    TestTableService testTableService;

    @GetMapping("/read")
    public Object read(){
        return readService.read();
    }


    @PostMapping("/user/list")
    public Object userList(){
        return userService.findAll();
    }

    @PostMapping("/testtable/list")
    public Object testtableList(){
        return testTableService.findAll();
    }

    @PostMapping("/testtable/{id}")
    public Object testtableById(@PathVariable("id") Long id){
        return testTableService.getOne(id);
    }

    @PostMapping("/testtable/add")
    public String testtableAdd(@RequestBody TestTable t){
        try {
            testTableService.add(t);
        }catch (Exception e){
            e.printStackTrace();
            return "fail";
        }
        return "ok";
    }

    @PostMapping("/user/add")
    public String userAdd(@RequestBody User user){
        try {
            userService.add(user);
        }catch (Exception e){
            e.printStackTrace();
            return "fail";
        }
        return "ok";
    }
}
